# Copyright (C) 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//tools/build:rules.bzl", "android_dynamic_library", "api_template", "apic_template", "cc_copts", "cc_dynamic_library")

api_template(
    name = "memory_tracker",
    includes = [
        "//gapis/api/vulkan/templates:vulkan_layer",
    ],
    outputs = [
        "layer.h",
        "layer.cpp",
        "allocation_callbacks_impl.cpp",
    ],
    template = "memory_tracker_layer.tmpl",
)

apic_template(
    name = "memory_tracker_templated",
    api = "//gapis/api/vulkan:api",
    templates = [
        ":memory_tracker",
    ],
)

cc_library(
    name = "cc",
    srcs = glob([
        "*.cpp",
        "*.h",
    ]) + [
        ":memory_tracker_templated",
    ],
    copts = cc_copts() + select({
        "//tools/build:linux": [
            "-DVK_USE_PLATFORM_XCB_KHR",
        ],
        "//tools/build:darwin": [],
        "//tools/build:darwin_arm64": [],
        "//tools/build:windows": ["-DVK_USE_PLATFORM_WIN32_KHR"],
        # Android
        "//conditions:default": ["-DVK_USE_PLATFORM_ANDROID_KHR"],
    }) + [
        "-fno-rtti",
        "-fno-exceptions",
        "-DNDEBUG",  # always ndebug for perfetto
    ],
    linkopts = select({
        "//tools/build:linux": ["-lpthread"],
        "//tools/build:darwin": [],
        "//tools/build:darwin_arm64": [],
        "//tools/build:windows": ["-lpthread"],
        # Android
        "//conditions:default": [
            "-ldl",
            "-lm",
            "-llog",
        ],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "//core/cc",
        "//core/vulkan/layer_helpers",
        "//core/vulkan/perfetto_producer",
        "@cityhash",
        "@vulkan-headers//:vulkan",
    ],
)

cc_library(
    name = "headers",
    srcs = glob([
        "*.h",
    ]),
    copts = cc_copts() + select({
        "//tools/build:linux": [
            "-DVK_USE_PLATFORM_XCB_KHR",
        ],
        "//tools/build:darwin": [],
        "//tools/build:darwin_arm64": [],
        "//tools/build:windows": ["-DVK_USE_PLATFORM_WIN32_KHR"],
        # Android
        "//conditions:default": ["-DVK_USE_PLATFORM_ANDROID_KHR"],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "@vulkan-headers//:vulkan",
    ],
)

cc_dynamic_library(
    name = "libVkLayer_MemoryTracker",
    visibility = ["//visibility:public"],
    exports = "memory_tracker_desktop.exports",
    deps = [":cc"],
)

android_dynamic_library(
    name = "libVkLayer_MemoryTracker_android",
    visibility = ["//visibility:public"],
    exports = "memory_tracker_android.exports",
    deps = [":cc"],
)

filegroup(
    name = "json",
    srcs = [
        "MemoryTrackerLayer.json",
    ],
    visibility = ["//visibility:public"],
)
